﻿using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Utility
{
    public static class CacheHelperNetCore
    {
        public static IMemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions());
        /// <summary>
        /// 设置key值缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj">值</param>
        /// <param name="timeSpan">过期时间</param>
        public static void SetCache(string key,object obj,TimeSpan timeSpan)
        {
            _memoryCache.Set(key,obj,timeSpan);
        }
        /// <summary>
        /// 获取key值缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetCache(string key)
        {
            return _memoryCache.Get(key);
        }
        /// <summary>
        /// 获取key值缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetCache<T>(string key)
        {
            return _memoryCache.Get<T>(key);
        }
        /// <summary>
        /// 该key值缓存是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool Exist(string key)
        {
            return _memoryCache.TryGetValue(key,out _);
        }
        /// <summary>
        /// 该key值缓存是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool Exist(string key,out object value)
        {
            return _memoryCache.TryGetValue(key, out value);
        }
    }
}
    